package com.spzx.user.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.DateUtils;
import com.spzx.user.domain.Region;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService
{
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private RegionService regionService;

    //根据用户id查询用户地址列表
    @Override
    public List<UserAddress> getList() {
        Long userId = SecurityContextHolder.getUserId();

        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUserId,userId);
        List<UserAddress> userAddressList = userAddressMapper.selectList(wrapper);
        return userAddressList;
    }

    //修改用户信息
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();
        userAddress.setUserId(userId);
//        int update = userAddressMapper.update(userAddress, new LambdaUpdateWrapper<UserAddress>().eq(UserAddress::getUserId,userId));
        return  userAddressMapper.updateById(userAddress);

    }

    //新增用户信息
    @Override
    public int saveUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();

          userAddress.setUserId(userId);
//        String provinceName = regionService.getlist1(userAddress.getProvinceCode());
//        String cityName = regionService.getlist1(userAddress.getCityCode());
//        String districtCodeName = regionService.getlist1(userAddress.getDistrictCode());
//        String fullAdress = provinceName+cityName+districtCodeName+userAddress.getAddress();
//        userAddress.setFullAddress(fullAdress);
//        userAddress.setCreateTime(new Date());
        int raw = userAddressMapper.insert(userAddress);
        //如果是默认地址，则把其他的改成非默认地址
        System.out.println(userAddress);

        if(userAddress.getIsDefault().intValue() == 1){
            UserAddress userAddress1 = new UserAddress();
            userAddress1.setIsDefault(0L);
            userAddressMapper.update(userAddress1,new LambdaQueryWrapper<UserAddress>()
                    .eq(UserAddress::getUserId,userAddress.getUserId()));
        }
        return raw;
    }
//
//    /**
//     * 查询用户地址列表
//     *
//     * @param userAddress 用户地址
//     * @return 用户地址
//     */
//    @Override
//    public List<UserAddress> selectUserAddressList(UserAddress userAddress)
//    {
//        return userAddressMapper.selectUserAddressList(userAddress);
//    }
//
//    //根据用户id获取用户收货地址
//    @Override
//    public List<UserAddress> selectUserIdAddressList() {
//        Long userId = SecurityContextHolder.getUserId();
//        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(UserAddress::getUserId,userId);
//        List<UserAddress> userAddressList = userAddressMapper.selectList(wrapper);
//
//        return userAddressList;
//    }

}
